<?php
namespace User\Controller;
use Think\Controller;
/**
 * aws接口调用控制器 AwsController.class.php
 */
class AwsController extends Controller{
    /* 属性声明 */
    private $url = null;
    
    /* 方法声明 */
    public function __construct() {
        parent::__construct();
        $this -> access_key = C('AWD_KEY');
        $this -> secret = C('AWD_SECRET');
        $this -> request_url = C('AWS_URL').'openapi';
        //初始化变量区域
        $baseurl = C('RPOJECT_URL');
        $baseurl = 'http://10.0.29.29/';
        $this -> url = $baseurl . 'cgi/api/approval';
    }
    
    /**
     * 连接aws生成待办接口
     */
    public function dealt($uuid,$username){
        $this -> url = $this -> url . '/uuid/' . $uuid;
        //当前时间戳 毫秒级
        $timestamp = round(microtime(true)*1000);
        //生成json数据
        $json = "{'username':'{$username}','url':'{$this->url}'}";
        //构造签名串
        $code = "{$this->secret}access_key{$this->access_key}cmdhrms.createAuditTaskformatjsonsig_methodHmacMD5timestamp{$timestamp}zparams{$json}";
        //生成HMACMD5签名
        $sig = strtoupper(hash_hmac('md5',$code,$this->secret));
        //组织请求url
        $url = 
        "{$this->request_url}?timestamp={$timestamp}&sig_method=HmacMD5&cmd=hrms.createAuditTask&access_key={$this->access_key}&format=json&sig={$sig}&zparams={$json}";
        $result = json_decode(file_get_contents($url),true);
        if($result['result']=='ok'){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 提交员工信息审批结果
     * @param string 提交json字符串
     */
    public function terminal($data){
        $cmd = 'hrms.updateUserInfo';
        //当前时间戳 毫秒级
        $timestamp = round(microtime(true)*1000);
        //生成json数据
        $json = $data;
        //构造签名串
        $code = "{$this->secret}access_key{$this->access_key}cmd{$cmd}formatjsonsig_methodHmacMD5timestamp{$timestamp}zparams{$json}";
        //生成HMACMD5签名
        $sig = strtoupper(hash_hmac('md5',$code,$this->secret));
        //组织请求url
        $url = 
        "{$this->request_url}?timestamp={$timestamp}&sig_method=HmacMD5&cmd={$cmd}&access_key={$this->access_key}&format=json&sig={$sig}&zparams={$json}";
        $result = json_decode(file_get_contents($url),true);
        if($result['result']=='ok'){
            return $result['data'];
        }else{
            return false;
        }
    }
    
    public function HmacMd5($data,$key){   
        //需要配置环境支持iconv，否则中文参数不能正常处理   
        $key = iconv("GB2312","UTF-8",$key);   
        $data = iconv("GB2312","UTF-8",$data);   
        $b = 64; // byte length for md5   
        if (strlen($key) > $b) {   
            $key = pack("H*",md5($key));   
        }   
        $key = str_pad($key, $b, chr(0x00));   
        $ipad = str_pad('', $b, chr(0x36));   
        $opad = str_pad('', $b, chr(0x5c));   
        $k_ipad = $key ^ $ipad ;   
        $k_opad = $key ^ $opad;   
        return md5($k_opad . pack("H*",md5($k_ipad . $data)));   
    } 
}